package cn.wolfcode.luowowo.mgrsite;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import cn.wolfcode.luowowo.cache.vo.TravelStrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class StatsDataPersistenceJob {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Reference
    private ITravelService travelService;
    @Scheduled(cron = "0/10 * * * * ?")
    public void persistence(){
        //核心:从redis中查出数据,同步到mysql中
        //从redis查询所有文章攻略的信息
        List<StrategyStats> vos = strategyStatsCacheService.listAllStrateg();
        //遍历vos,将其封装成文章攻略对象
        for (StrategyStats vo : vos) {
            StrategyDetail detail = new StrategyDetail();
            detail.setId(vo.getStrategyId());
            detail.setViewnum(vo.getViewnum());
            detail.setReplynum(vo.getReplynum());
            detail.setFavornum(vo.getFavornum());
            detail.setThumbsupnum(vo.getThumbsupnum());
            detail.setSharenum(vo.getSharenum());
            //更新到mysql
            strategyDetailService.updateState(detail);
        }

        //从redis中查询所有游记
        List<TravelStats> tvos = travelStatsCacheService.listAllTravel();
        //遍历vos,将其封装成游记对象
        for (TravelStats vo : tvos) {
           Travel travel = new Travel();
            travel.setId(vo.getTravelId());
            travel.setViewnum(vo.getViewnum());
            travel.setReplynum(vo.getReplynum());
            travel.setFavornum(vo.getFavornum());
            travel.setThumbsupnum(vo.getThumbsupnum());
            travel.setSharenum(vo.getSharenum());
            //更新到mysql
            travelService.updateState(travel);
        }
    }
}
